package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
        // 登陆拦截器,后执行
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
                .excludePathPatterns(  // 添加拦截路径
                        "/user/code",
                        "/user/login",
                        "/blog/hot",
                        "/shop/**",  // 放行 shop 打头的全部路径
                        "/shop-type/**",
                        "/upload/**",
                        "/voucher/**",
                        "/try/**"
                )
                .order(2);  // 数字越小越先执行
        // token刷新的拦截器,先执行
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns(
                        "/**"  // 默认也是这个
                )
                .order(0);

    }
}
